package com.bj58.mis.apm.server.logreceiver.controller;

import com.bj58.mis.apm.server.core.common.result.ResultBean;
import com.bj58.mis.apm.server.logreceiver.service.DictionaryService;
import com.bj58.mis.apm.server.logreceiver.vo.DictionaryItemVO;
import com.bj58.mis.apm.server.logreceiver.vo.DictionaryTypeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wangxw
 * @Date: 2018/8/20 16:39
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    @RequestMapping("/types")
    public ResultBean<List<DictionaryTypeVO>> queryTypes(){
        return ResultBean.success(dictionaryService.queryTypes());
    }
    @RequestMapping("/items")
    public ResultBean<List<DictionaryItemVO>> queryItemsByType(@RequestParam(name="typeId", required = true) String typeId){
        if(StringUtils.isEmpty(typeId)) {
            return ResultBean.success(new ArrayList<>());
        }
        return ResultBean.success(dictionaryService.queryItems(typeId));
    }

    @RequestMapping("/saveItems")
    public void saveItems(@RequestBody List<DictionaryItemVO> itemList){
        dictionaryService.saveItems(itemList);
    }
}
